package cn.zkw.service.impl;

import cn.zkw.mapping.IUserDao;
import cn.zkw.service.UserService;
import cn.zkw.vo.Label;
import cn.zkw.vo.User;
import cn.zkw.vo.User_like;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public IUserDao dao;

    @Override
    public User getUserByName(String user_name) {
        return dao.findUserByName(user_name);
    }

    @Override
    public boolean updateNikeName(User user) {
        return dao.updateUserNameById(user)>=1;
    }

    public boolean addUser(User user){
        return dao.addUser(user)>=1;
    }

    @Override
    public boolean updateSexById(User user) {
        return dao.updateSexById(user)>=1;
    }

    @Override
    public boolean updateBirthdayById(User user) {
        return dao.updateBirthdayById(user)>=1;
    }

    @Override
    public Integer addUser_like_OR_inreaseClick_Count(Map<String, Integer> map) {
        List<User_like> list = selectLikeByUserId(map.get("user_id"));  //查找用户的所有喜好
        boolean exist = false;
        for(int i=0;i<list.size();i++){
            User_like label = list.get(i);
            if(label.getLabel_id()==map.get("label_id")){  //如果用户要添加的用户喜好不存在，则可以添加喜好
                exist = true;
            }
        }
        if (exist){ //存在喜好就增加1，不存在就添加
            dao.increaseClick_Count(map); //增加用户喜好的权值
        }else{
            dao.addUser_like(map);
        }
        return 1;
    }

    @Override
    public List<User_like> selectLikeByUserId(Integer user_id) {
        List<User_like> list = dao.selectLikeByUserId(user_id);
        Collections.sort(list);
        return list;
    }

    @Override
    public Integer minusClick_count(Map<String, Integer> map) {
        return dao.minusClick_count(map);
    }

}
